package com.sgb.member.view_model;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.member.MemberConfig;
import com.sgb.member.entity.MemberLeverBean;
import com.sgb.member.service.MemberAPI;
import com.sgb.member.weight.SimpleResult;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class MemberLeverViewModel extends AndroidViewModel {
    public MutableLiveData<Boolean> dialog = new MutableLiveData<>();
    public MutableLiveData<Boolean> empty = new MutableLiveData<>();
    public MutableLiveData<List<MemberLeverBean>> mLeverData = new MutableLiveData<>();
    public MutableLiveData<Boolean> mLeverUpdate = new MutableLiveData<>();
    private final MemberAPI api;

    public MemberLeverViewModel(@NonNull Application application) {
        super(application);
        api = MemberConfig.getInstance().getApi();
    }

    public void getRatLeverList() {
        dialog.setValue(true);
        api.getRatLevel().enqueue(new ICallBack<BaseEntity<List<MemberLeverBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<MemberLeverBean>> data) {
                mLeverData.setValue(data.getData());
                empty.setValue(false);
                dialog.setValue(false);
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MemberLeverBean>>> call, Throwable t) {
                super.onFailure(call, t);
                empty.setValue(true);
                dialog.setValue(false);
            }
        });
    }

    public void getH5Url(int id, SimpleResult<String> simpleResult) {
        dialog.setValue(true);
        api.getUrlSetting("gradeRegulation").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                dialog.setValue(false);
                String url = data.getData().getUrl() + "?id=" + id;
                MLog.d("api", "H5跳转链接  " + url);
                simpleResult.onResult(url);
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                super.onFailure(call, t);
                dialog.setValue(false);
            }
        });
    }
}
